<template>
  <div>
    <nav-bar title="扫一扫"></nav-bar>
    <div class="apply backgroundfff" v-if="isShowScan">
      <svg-icon class="qcCode" icon-class="qrCode" @click.native="sweep" />
    </div>
    <div id="code" style="width:100%;height:80%; background-color: #fff; text-align: center;position: absolute;left: 0;top: 5.375rem" >
    </div>
    <p class="center">请扫描项目二维码</p>
  </div>
</template>

<script>
  import myAPI from '../../../api/myAPI.js'
  import dashbordAPI from '../../../api/dashbordAPI.js'
  let Base64 = require('js-base64').Base64
  export default {
    data() {
      return {
        isShowScan: true,
        scan: '',
      }
    },
    mounted() {
      const self = this
      if (window.plus) {
        self.plusReady()
      } else {
        document.addEventListener('plusready', self.plusReady, false);
      }
      document.addEventListener('DOMContentLoaded', function() {
        alert('DOMLoaded')
        self.domready = true;
        self.plusReady()
      }, false)
    },
    destroyed() {
      this.closeScan()
    },
    methods: {
      // 关闭二维码
      closeScan() {
        if (!window.plus) return;
        this.scan.close();
      },
      plusReady() {
        const self = this
        // 获取窗口
        self.scan = new plus.barcode.Barcode('code');
        self.scan.onmarked = self.onmarked;
        self.scan.start();
      },

      sweep() {
        this.isShowScan = false
        const self = this
        self.scan.start();
      },
      openDialog(text) {
        this.$dialog.alert({
          title: '扫描结果',
          message: text,
          className: 'dialog'
        }).then(() => {
          this.sweep()
        }).catch(() => {
          this.sweep()
        });
      },
      onmarked(type, result) {
        if (result.indexOf(':') !== -1) {
          let projectMessage = JSON.parse(Base64.decode(result.slice(result.lastIndexOf(':') + 1)))
          if (projectMessage.id) {
            this.closeScan()
            localStorage.setItem('projectId', projectMessage.id)
            myAPI.getUserInfo().then(resUserInfo => {
              if (resUserInfo.id) {
                localStorage.setItem('userStatus', resUserInfo.status)
                if (localStorage.getItem('userType') === '02' && resUserInfo.status === 4) {
                  this.$router.push({
                    name: 'Dashbord'
                  })
                }
                dashbordAPI.getWorkTest(localStorage.getItem('projectId')).then(resMessage => {
                  if (resMessage && resMessage.id) {
                    if (resMessage.passExam) {
                      this.$router.push({
                        name: 'Dashbord'
                      })
                    } else {
                        this.$router.push({
                          name: 'ApplyMessage',
                          params: {
                            value: 2
                          }
                        })
                      }
                  } else {
                    this.$router.push({
                      name: 'ApplyMessage',
                      params: {
                        value: 1,
                        buildId: resUserInfo.id
                      }
                    })
                  }
                })
              } else {
                localStorage.removeItem('form');
                this.$router.push({
                  name: 'ApplyMessage',
                  params: {
                    value: 0
                  }
                })
              }
            })
          } else {
            this.openDialog('请扫描正确的二维码')
          }
        } else {
          this.openDialog('扫描错误,请重新二维码')
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .apply{
    padding: 3.125rem;
    margin-top: 6.25rem;
    text-align: center;
    .qcCode{
      font-size: 3.125rem;
    }
  }
  /deep/ .van-dialog {
    position: fixed;
    top: 60%
  }
</style>
